'''
根据http协议完成
通过浏览器访问服务端,请求则会会显示一张图片在浏览器上
Content-Type:image/jpeg
'''
from socket import *

file = open('028879851afcd0c72bc49dd40042a3da.jpg', 'rb')
data = file.read()
sock = socket()
sock.bind(('0.0.0.0', 8881))
sock.listen(5)
while True:
    # 等待浏览器连接
    connfd, addr = sock.accept()
    print('connect from', addr)
    # 接受HTTP请求
    request = connfd.recv(1024)
    print(request.decode())

    # 组织响应格式
    response = 'HTTP/1.1 200 ok\n'
    response += 'Content-Type:image/jpeg\n'
    response += '\n'

    connfd.send(response.encode() + data)

# connfd.close()
# sock.close()
